/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.CourseClockConfig;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.CourseClockConfigRepository;
import me.zhengjie.modules.system.service.CourseClockConfigService;
import me.zhengjie.modules.system.service.dto.CourseClockConfigDto;
import me.zhengjie.modules.system.service.dto.CourseClockConfigQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.CourseClockConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2023-10-19
**/
@Service
@RequiredArgsConstructor
public class CourseClockConfigServiceImpl implements CourseClockConfigService {

    private final CourseClockConfigRepository courseClockConfigRepository;
    private final CourseClockConfigMapper courseClockConfigMapper;

    @Override
    public Map<String,Object> queryAll(CourseClockConfigQueryCriteria criteria, Pageable pageable){
        Page<CourseClockConfig> page = courseClockConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(courseClockConfigMapper::toDto));
    }

    @Override
    public List<CourseClockConfigDto> queryAll(CourseClockConfigQueryCriteria criteria){
        return courseClockConfigMapper.toDto(courseClockConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CourseClockConfigDto findById(Long clockConfigId) {
        CourseClockConfig courseClockConfig = courseClockConfigRepository.findById(clockConfigId).orElseGet(CourseClockConfig::new);
        ValidationUtil.isNull(courseClockConfig.getClockConfigId(),"CourseClockConfig","clockConfigId",clockConfigId);
        return courseClockConfigMapper.toDto(courseClockConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CourseClockConfigDto create(CourseClockConfig resources) {
        return courseClockConfigMapper.toDto(courseClockConfigRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CourseClockConfig resources) {
        CourseClockConfig courseClockConfig = courseClockConfigRepository.findById(resources.getClockConfigId()).orElseGet(CourseClockConfig::new);
        ValidationUtil.isNull( courseClockConfig.getClockConfigId(),"CourseClockConfig","id",resources.getClockConfigId());
        courseClockConfig.copy(resources);
        courseClockConfigRepository.save(courseClockConfig);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long clockConfigId : ids) {
            courseClockConfigRepository.deleteById(clockConfigId);
        }
    }

    @Override
    public void download(List<CourseClockConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CourseClockConfigDto courseClockConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程编号", courseClockConfig.getCourseNo());
            map.put("打卡开始时间", courseClockConfig.getStartDate());
            map.put("打开结束时间", courseClockConfig.getEndDate());
            map.put("全员可见：0，班级可见：1", courseClockConfig.getVisibleAuth());
            map.put("内容是否需要审核，0：不需要，1：需要", courseClockConfig.getRequiredAudit());
            map.put("状态，0：无效，1：有效", courseClockConfig.getStatus());
            map.put("创建时间", courseClockConfig.getCreateTime());
            map.put("创建人", courseClockConfig.getCreateby());
            map.put("更新时间", courseClockConfig.getUpdateTime());
            map.put("更新人", courseClockConfig.getUpdateby());
            map.put("打卡规则", courseClockConfig.getClockRule());
            map.put("课程名称", courseClockConfig.getCourseName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}